<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PersonContractController extends Controller
{
	public function getIndex(Request $request)
	{
		//当前时间戳
		$time = time();
		$time1 = date("Y-m-d H:i:s", $time);
		$time11 = substr($time1,0,10);
		// dd($time11);
		if($request['name']){
			$pinfo = DB::table('Personcontract')
			->where('PName','like','%'.$request['name'].'%')
            ->paginate(10);
		}else{
			$pinfo = DB::table('Personcontract')
            ->paginate(3);
		}

			foreach($pinfo as $k=>$v){
				//数据库如果不是这个字段 执行 当前时间大于数据库时间 失效 状态改为1 
				 if($v['ExpirationDate'] != "无固定期限"){
				 	$etime = $v['ExpirationDate'];
				 	if($time11 > $etime){
				 		$linfo = DB::select("update personcontract set State = 1 where ExpirationDate < '{$time11}'");
				 	}
				 	if($time11 < $etime){
				 		$linfo = DB::select("update personcontract set State = 2 where ExpirationDate > '{$time11}'");
				 	}
				 }
			}
			 $info = $request -> all();
		return view('Lzk.PersonContract.PersonContract',['pinfo'=>$pinfo,'info'=>$info]);

	}

	public function getAdd()
	{
		return view('Lzk.PersonContract.AddPersonContract');
	}

	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->except('_token');
		$pname = $ins['PName'];
		$name = DB::table('person')->where('PersonName',$pname)->first();
		// dd($name);
		if(!$name){
			echo '<script>alert("请填写正确的员工名");window.location.href="/Lzk/PersonContract/add";</script>';
			die;
		}

		$pername = DB::table('Personcontract')->where('PName',$pname)->first();
		// dd($pername);
		if($pername){
			echo '<script>alert("此员工以签订合同");window.location.href="/Lzk/PersonContract/add";</script>';
			die;
		}



		$ins['Date'] = time();
		//获取开始时间
		$start = $ins['EffectiveDate'];
		// dd($start);
		//获取结束时间
		$outdate = $ins['ExpirationDate'];
		// 把开始时间成时间戳
		$statime = strtotime($start);
		// dd($a);

		$v1 = 180*24*60*60; //6个月的时间戳
		$v2 = 365*24*60*60; //一年的时间戳
		$v3 = 1095*24*60*60; //三年的时间戳
		$v4 = 1825*24*60*60; //5年的时间戳

		$otime = 0;
		if($outdate == 1){
			$otime = $v1 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 2){
			$otime = $v2 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 3){
			$otime = $v3 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 4){
			$otime = $v4 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 5){
			$outime = "无固定期限";
		}
		// dd($outime);
		$ins['ExpirationDate'] = $outime;
		$ins['State'] = 2;
		
		$res = DB::table('Personcontract')->insert($ins);
		if($res){
			return redirect('Lzk/PersonContract');
		}else{
			return back();
		}
	}

	public function getLook($id)
	{
		$res = DB::table('Personcontract')->where('id',$id)->first();
		return view('Lzk.PersonContract.LookPersonContract',['res'=>$res]);
	}


	public function getDel($id)
	{
		$res = DB::table('Personcontract')->where('id',$id)->first();
		// dd($res);
		$state = $res['State'];
		// dd($state);
		if($state == 2){
			echo '<script>alert("此合同还未到期,请勿删除");window.location.href="/Lzk/PersonContract";</script>';
			// die;

		}else{
			$del = DB::table('Personcontract')->where('id',$id)->delete($id);
			if($del){
				return redirect('Lzk/PersonContract');
			}else{
				return back();
			}
		}
	}

	public function getEdit($id)
	{
		$einfo = DB::table('Personcontract')->where('id',$id)->first();
		// dd($einfo);
		return view('Lzk.PersonContract.EditPersonContract',['einfo'=>$einfo]);
	}

	public function postUpdate(Request $request)
	{
		// dd($request->all());
		$id = $request->only('id');
		$uinfo = $request->only('EffectiveDate','ExpirationDate');
		$start = $uinfo['EffectiveDate'];
		// dd($start);
		//获取结束时间
		$outdate = $uinfo['ExpirationDate'];
		// dd($outdate);
		// 把开始时间成时间戳
		$statime = strtotime($start);

		$v1 = 180*24*60*60; //6个月的时间戳
		$v2 = 365*24*60*60; //一年的时间戳
		$v3 = 1095*24*60*60; //三年的时间戳
		$v4 = 1825*24*60*60; //5年的时间戳

		$otime = 0;
		if($outdate == 1){
			$otime = $v1 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 2){
			$otime = $v2 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 3){
			$otime = $v3 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 4){
			$otime = $v4 + $statime;
			$outime = date("Y-m-d H:i:s", $otime) ;
		}

		if($outdate == 5){
			$outime = "无固定期限";
		}
		// dd($outime);
		$uinfo['ExpirationDate'] = $outime;
		$uinfo['State'] = 2;
		// dd($uinfo);
		$res = DB::table('Personcontract')->where('id',$id)->update($uinfo);
		if($res){
				return redirect('Lzk/PersonContract');
			}else{
				return back();
			}

	}
}
 